package com.xuecheng.order.mq;

import com.xuecheng.framework.domain.task.XcTask;
import com.xuecheng.order.config.RabbitMQConfig;
import com.xuecheng.order.service.TaskService;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.jpa.repository.Query;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import javax.persistence.GenerationType;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.List;

@Component
public class ChooseCourseTask {

    @Autowired
    TaskService taskService;
    @Scheduled(cron="0/10 * * * * *")
    public void sendChoosecourseTask(){
        //取出一分钟前的数据
        Calendar calendar =new GregorianCalendar();
        calendar.setTime(new Date());
        calendar.add(GregorianCalendar.MINUTE,calendar.get(GregorianCalendar.MINUTE)-1);
        Date time = calendar.getTime();
        List<XcTask> taskList = taskService.findTaskList(time, 100);
        for (XcTask xcTask : taskList) {
            Integer version = xcTask.getVersion();
            String id = xcTask.getId();
            int task = taskService.getTask(id, version);
            if(task>0){
                //发送选课消息
                taskService.publish(xcTask,xcTask.getMqExchange(),xcTask.getMqRoutingkey());
            }
        }
    }
    @RabbitListener(queues = {RabbitMQConfig.XC_LEARNING_FINISHADDCHOOSECOURSE})
    public void receiveFinishChoosecourseTask(XcTask xcTask){
        taskService.deleteTask(xcTask.getId());

    }
}
